package com.fuyao.cloud.admin.flow.service.flowable.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.admin.flow.mapper.IExtendProcinstMapper;
import com.fuyao.cloud.admin.flow.model.ExtendHisprocinst;
import com.fuyao.cloud.admin.flow.model.ExtendProcinst;
import com.fuyao.cloud.admin.flow.service.flowable.IExtendHisprocinstService;
import com.fuyao.cloud.admin.flow.service.flowable.IExtendProcinstService;
import com.fuyao.cloud.common.core.flowable.dto.StartProcessInstanceParamsDTO;
import com.fuyao.cloud.common.core.flowable.enums.ProcessStatusEnum;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 */
@Service
public class ExtendProcinstServiceImpl extends ServiceImpl<IExtendProcinstMapper, ExtendProcinst> implements IExtendProcinstService {

    @Lazy
    @Autowired
    private IExtendHisprocinstService extendHisprocinstService;
    @Autowired
    private HistoryService historyService;

    @Override
    public void deleteExtendProcinstByProcessInstanceId(String processInstanceId) {
        LambdaQueryWrapper<ExtendProcinst> extendProcinstLambdaQueryWrapper = new LambdaQueryWrapper<>();
        extendProcinstLambdaQueryWrapper.eq(ExtendProcinst::getProcessInstanceId,processInstanceId);
        this.remove(extendProcinstLambdaQueryWrapper);
    }

    @Override
    public void saveExtendProcinstAndHis(ExtendProcinst extendProcinst) {
        extendProcinst.setId(IdWorker.get32UUID());
        extendProcinst.setCreateTime(new Date());
        extendProcinst.setUpdateTime(new Date());
        this.save(extendProcinst);
        ExtendHisprocinst extendHisprocinst = new ExtendHisprocinst();
        BeanUtils.copyProperties(extendProcinst, extendHisprocinst);
        this.extendHisprocinstService.save(extendHisprocinst);
    }

    @Override
    public void updateStatus(ProcessStatusEnum processStatus, String processInstanceId) {
        LambdaUpdateWrapper<ExtendProcinst> extendProcinstLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        extendProcinstLambdaUpdateWrapper.set(ExtendProcinst::getProcessStatus, processStatus.getType())
                .eq(ExtendProcinst::getProcessInstanceId,processInstanceId);
        this.update(extendProcinstLambdaUpdateWrapper);
        LambdaUpdateWrapper<ExtendHisprocinst> extendHisprocinstLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        extendHisprocinstLambdaUpdateWrapper.set(ExtendHisprocinst::getProcessStatus, processStatus.getType())
                .eq(ExtendHisprocinst::getProcessInstanceId,processInstanceId);
        extendHisprocinstService.update(extendHisprocinstLambdaUpdateWrapper);
    }

    @Override
    public ExtendProcinst findExtendProcinstByProcessInstanceId(String processInstanceId) {
        LambdaQueryWrapper<ExtendProcinst> extendProcinstLambdaQueryWrapper = new LambdaQueryWrapper<>();
        extendProcinstLambdaQueryWrapper.eq(ExtendProcinst::getProcessInstanceId, processInstanceId);
        return this.getOne(extendProcinstLambdaQueryWrapper);
    }


    @Override
    public String createExtendProcinst(StartProcessInstanceParamsDTO params, ProcessInstance processInstance, String creator) {
        ExtendProcinst extendProcinst = new ExtendProcinst();
        extendProcinst.setCurrentUserCode(params.getCreator());
        extendProcinst.setProcessInstanceId(processInstance.getId());
        extendProcinst.setProcessDefinitionId(processInstance.getProcessDefinitionId());
        extendProcinst.setProcessStatus(ProcessStatusEnum.SPZ.toString());
        extendProcinst.setProcessName(params.getFormName());
        extendProcinst.setModelKey(params.getProcessDefinitionKey());
        extendProcinst.setBusinessKey(params.getBusinessKey());
        extendProcinst.setTenantId(params.getTenantId());
        extendProcinst.setCreator(creator);
        String processStatus = extendProcinst.getProcessStatus();
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getProcessInstanceId()).singleResult();
        if (historicProcessInstance.getEndTime() != null){
            extendProcinst.setProcessStatus(ProcessStatusEnum.BJ.toString());
            extendProcinst.setProcessName(params.getFormName());
            extendProcinst.setBusinessKey(params.getBusinessKey());
            extendProcinst.setProcessDefinitionId(historicProcessInstance.getProcessDefinitionId());
            extendProcinst.setModelKey(params.getProcessDefinitionKey());
            extendProcinst.setTenantId(params.getTenantId());
            extendProcinst.setCurrentUserCode(params.getCreator());
            extendProcinst.setId(IdWorker.get32UUID());
            extendProcinst.setCreateTime(new Date());
            extendProcinst.setUpdateTime(new Date());
            ExtendHisprocinst extendHisprocinst = new ExtendHisprocinst();
            BeanUtils.copyProperties(extendProcinst, extendHisprocinst);
            extendHisprocinstService.save(extendHisprocinst);
            processStatus = extendHisprocinst.getProcessStatus();
        } else {
            saveExtendProcinstAndHis(extendProcinst);
        }
        return processStatus;
    }
}
